﻿using System;
using System.Collections.Generic;
using System.Transactions;
using Bingosoft.Data;
using HHMMS.UI.Common.Models;

namespace HHMMS.UI.Common
{
    public class AttachmentService
    {
        private readonly Dao _dao = Dao.Get();

        public AttachmentInfo GetAttachmentInfo(String Id)
        {
            var param = new Dictionary<string, object>() { { "Id", Id } };
            return this._dao.QueryEntity<AttachmentInfo>("Common.AttachmentInfo.Get", param);                
        }

        public String SaveOrUpdateAttachmentInfo(AttachmentInfo attachmentInfo)
        {
            using (TransactionScope transactionScope = new TransactionScope(TransactionScopeOption.Required))
            {
                if (string.IsNullOrEmpty(attachmentInfo.Id))
                {
                    attachmentInfo.Id = Guid.NewGuid().ToString();
                    this._dao.ExecuteNonQuery("Common.AttachmentInfo.Insert", attachmentInfo);
                }
                else
                {
                    this._dao.ExecuteNonQuery("Common.AttachmentInfo.Update", attachmentInfo);
                }
                transactionScope.Complete();
            }
            return attachmentInfo.Id;
        }

        public void DeleteAttachmentInfo(string Id)
        {
            using (TransactionScope transactionScope = new TransactionScope())
            {
                var param = new Dictionary<string, object>() { { "Id", Id } };
                this._dao.ExecuteNonQuery("Common.AttachmentInfo.Delete", param);
                transactionScope.Complete();
            }

        }
        
    }
}